﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace CopyRightCodeCollector {

    public class Program {

        static void ShowHelp() {
            var content = File.ReadAllText("help.txt");
            Console.WriteLine(content);
        }

        static (string, string, string, string) ParseArguements(string[] args) {
            if (args.Length < 2) {
                ShowHelp();
                return (null, null, null, null);
            }

            var srcDir = args[0];
            var destDir = args[1];
            var entryPoint = args.Length > 2 ? args[2] : null;
            var configFile = args.Length > 3 ? args[3] : "config.yml";
            return (srcDir, destDir, entryPoint, configFile);
        }

        public static void OUTPUT(string filepath, IEnumerable<string> lines) {
            var folder = Path.GetDirectoryName(filepath);
            if (!Directory.Exists(folder))
                Directory.CreateDirectory(folder);
            File.WriteAllLines(filepath, lines, Encoding.UTF8);
        }

        public static void Main(string[] args) {
            (var srcDir, var destDir, var entryPoint, var configFile) = ParseArguements(args);
            if (srcDir is null) return;
            try {
                using Collector pro = new(srcDir, destDir, entryPoint, configFile, OUTPUT);
                pro.Work();
            } catch (Exception err) {
                Console.WriteLine(err.ToString());
                ShowHelp();
            }
        }
    }
}